package com.cloud.diffdemo.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.cloud.diffdemo.R;
import com.cloud.diffdemo.model.TestBean;

import java.util.List;

/**
 * @auth: njb
 * @date: 2024/11/20 23:24
 * @desc: 测试信息适配器
 */
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder> {
    private final static String TAG = "TestAdapter";
    private List<TestBean> mData;
    private Context mContext;
    private LayoutInflater mInflater;

    public TestAdapter(Context mContext, List<TestBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<TestBean> mData) {
        this.mData = mData;
    }

    @Override
    public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TestViewHolder(mInflater.inflate(R.layout.item_test, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final TestViewHolder holder, final int position) {
        TestBean bean = mData.get(position);
        holder.mTvName.setText(bean.getName());
        holder.mTvContent.setText(bean.getContent());
        displayCircleImage(mContext, bean.getUrl(), holder.mImgUrl, 0);
        holder.mTvLookNumber.setText(new StringBuffer("浏览量：").append(bean.getLookNumber()));
        holder.mTvCommentNumber.setText(new StringBuffer("评论量：").append(bean.getCommentNumber()));
    }

    @Override
    public void onBindViewHolder(@NonNull TestViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            Bundle payload = (Bundle) payloads.get(0);
            TestBean bean = mData.get(position);
            for (String key : payload.keySet()) {
                switch (key) {
                    case "KEY_DESC":
                        holder.mTvContent.setText(payload.getString(key));
                        break;
                    case "KEY_PIC":
                        displayCircleImage(mContext, payload.getString(key), holder.mImgUrl, 0);
                        break;
                    case "KEY_LOOK_NUMBER":
                        holder.mTvLookNumber.setText(new StringBuffer("浏览量：").append(payload.getInt(key)));
                        break;
                    case "KEY_COMMENT_NUMBER":
                        holder.mTvCommentNumber.setText(new StringBuffer("评论量：").append(payload.getInt(key)));
                        break;
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    class TestViewHolder extends RecyclerView.ViewHolder {

        ImageView mImgUrl;
        TextView mTvName, mTvContent, mTvLookNumber, mTvCommentNumber;

        public TestViewHolder(View itemView) {
            super(itemView);
            mImgUrl = itemView.findViewById(R.id.img_url);
            mTvName = itemView.findViewById(R.id.tv_name);
            mTvContent = itemView.findViewById(R.id.tv_content);
            mTvLookNumber = itemView.findViewById(R.id.tv_look_number);
            mTvCommentNumber = itemView.findViewById(R.id.tv_comment_number);
        }
    }

    public static void displayCircleImage(Context context, String url, ImageView imageView, int placeHolderId) {
        RequestOptions options = (new RequestOptions()).circleCrop().placeholder(placeHolderId);
        Glide.with(context).load(url).transition(DrawableTransitionOptions.withCrossFade()).apply(options).into(imageView);
    }
}
